using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using System.IO;

using EntLibEC.Cmn;

using EntLibEC.Info.Basic;
using EntLibEC.Info.RMA;
using EntLibEC.Info.Stock;

using EntLibEC.Biz;
using EntLibEC.Biz.Basic;
using EntLibEC.Biz.RMA;

namespace EntLibEC.View.RMA
{
	/// <summary>
	/// Summary description for RequestReceivePrint.
	/// </summary>
	public partial class RequestReceivePrint : PageBase
	{

		private int sysno;
		protected string bodyOnLoad;

		private int lineInPage = 30;
		
	
		protected void Page_Load(object sender, System.EventArgs e)
		{
			if ( Request.QueryString["sysno"] == null )
			{
				Assert(lblMessage, "Request string missing", -1);
				return;
			}
			try
			{
				sysno = Int32.Parse(Request.QueryString["sysno"].ToString());
			}
			catch
			{
				Assert(lblMessage, "Request string error", -1);
				return;
			}

			bodyOnLoad = "javascript:window.print();window.close();";

		}
		protected string GetPrintContent()
		{
			
			string path = Server.MapPath(".");
			string fileName = "RequestReceivePrintTemplet.htm";
			string filePath = path + @"\" + fileName;

			//base.LineInPage = Int32.Parse(hidLineInPage.Value);

			FileStream aFile;
			try
			{
				aFile = new FileStream(@filePath, FileMode.Open, FileAccess.Read);
			}
			catch
			{
				Assert(lblMessage, "templet file open error", -1);
				return "";
			}

			StreamReader sr = new StreamReader(aFile,System.Text.Encoding.GetEncoding("gb2312"));

			string templet = sr.ReadToEnd();
			sr.Close();
			
			// ~ Get Company Info
			//1 prepare the information of head execept "pager"
			//2 get headTemplet
			//3 fill in head by replace the key string.
			

			//~
			EntLibInfo cInfo = new EntLibInfo();


			//1
			RMARequestInfo a = RMARequestManager.GetInstance().Load(this.sysno);
			if ( a == null )
			{
				Assert(lblMessage, "Load Request Info Error", -1);
				return "";
			}
			

			UserInfo CreateUser = SysManager.GetInstance().LoadUser(a.CreateUserSysNo);
			UserInfo RecvUser = SysManager.GetInstance().LoadUser(a.RecvUserSysNo);
			

			templet = Rep( templet, "_receiveDate", DateTime.Now.ToShortDateString());			
			templet = Rep( templet, "_soSysNo", a.SOSysNo.ToString());
			templet = Rep( templet, "_customerName", a.Contact);
			templet = Rep( templet, "_customerTel", a.Phone);			
			templet = Rep( templet, "_customerAdd", a.Address);		
			
			Hashtable itemHash = a.ItemHash;	

			// calc totalPage
			int totalPage= itemHash.Count / lineInPage;
			if ( itemHash.Count % lineInPage != 0 )
			{
				totalPage++;
			}
			
			string productInfo = "" ;
			string customerDesc = "";
			foreach(RMARegisterInfo item in itemHash.Values)
			{				

				ProductBasicInfo oProduct = new ProductBasicInfo();
				oProduct = ProductManager.GetInstance().LoadBasic(item.ProductSysNo);				

				if(productInfo == "")
					productInfo = oProduct.BriefName ;
				else
				    productInfo += "<br>" + oProduct.BriefName ;

				if(customerDesc == "")
					customerDesc = item.CustomerDesc ;
				else
				    customerDesc += "<br>" + item.CustomerDesc ;
			}


			templet = Rep(templet , "_productInfo" , productInfo);		
			templet = Rep(templet , "_customerDesc" , customerDesc);
			templet = Rep(templet , "_beginHead" , "");
			templet = Rep(templet , "_endHead" , "");


			return templet;

		}

		private string Rep(string org, string source, string target)
		{
			return Rep( org, source, target, 0);
		}
		private string Rep( string org, string source, string target, int len)
		{
			if ( len != 0 )
			{
				if ( target.Length>len )
				{
					target = target.Substring(0, len);
				}
			}
			return org.Replace(source, target);
		}
		private string RepField(string org, string beginTag, string endTag, string block)
		{
			int beginIndex, endIndex;
			string result;

			beginIndex = org.IndexOf( beginTag, 0 );
			endIndex	= org.IndexOf( endTag, 0 );
			if ( endIndex <= beginIndex )
			{
				return org;
			}
			else
			{
				result = org.Substring(0, beginIndex) + org.Substring( endIndex );
				result = result.Replace( endTag, block);
				return result;
			}
		}
		private string GetString(string org, string beginTag, string endTag)
		{
			return GetString(org, beginTag, endTag, "");
		}
		private string GetString(string org, string beginTag, string endTag, string repBegin)
		{
			int beginIndex, endIndex;
			string result;
			beginIndex	= org.IndexOf( beginTag, 0);
			endIndex		= org.IndexOf( endTag, 0);
			if (endIndex <= beginIndex)
			{
				return "";
			}
			else
			{
				result	= org.Substring( beginIndex, endIndex-beginIndex );
				result	= result.Replace( beginTag, repBegin);
				return result;
			}
		}
		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion
	}
}
